package hsp.admin.app.literature.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.literature.po.SayingPO;
import hsp.admin.app.literature.service.ISayingService;
import hsp.admin.app.literature.vo.SayingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "SayingController", description = "名言管理")
@RestController
@RequestMapping("literature/saying")
public class SayingController {

    private final ISayingService service;

    @Autowired
    public SayingController(
        ISayingService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取名言")
    @GetMapping("{id}")
    public ApiResult<SayingPO> getSaying(@PathVariable("id") long id){
        SayingPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<SayingVO>> getSayingPage(@PathVariable int page, @PathVariable int size, SayingPO po){
        Page<SayingVO> records = service.pageVO(new Page<>(page, size), po);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑名言")
    @PostMapping()
    public ApiResult<?> postOrPutSaying(@RequestBody SayingPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除名言")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteSaying(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
